package com.xiaoyu.lanling.feature.user.model

import `in`.srain.cube.request.JsonData
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.model.User.SEX_MAN
import com.xiaoyu.base.model.User.SEX_WOMAN
import com.xiaoyu.lanling.R

class UserPreference(val jsonData: JsonData) {

    companion object {
        const val KEY_SEX = "sex"
        const val KEY_VERIFY = "verify"
        const val KEY_MIN_AGE = "minAge"
        const val KEY_MAX_AGE = "maxAge"
        const val KEY_PROVINCE = "province"
        const val KEY_CITY = "city"

        const val VERIFY_ALL = "all"
        const val VERIFY_VERIFIED = "verified"

        const val MIN_AGE = 18
        const val MAX_AGE = 70
    }

    var sex: String = jsonData.optString(KEY_SEX, if (UserData.getInstance().user?.isMale == true) SEX_WOMAN else SEX_MAN)
    var verify: String = jsonData.optString(KEY_VERIFY, VERIFY_ALL)
    var minAge: String = jsonData.optString(KEY_MIN_AGE, MIN_AGE.toString())
    var maxAge: String = jsonData.optString(KEY_MAX_AGE, MAX_AGE.toString())
    var province: String = jsonData.optString(KEY_PROVINCE)
    var city: String = jsonData.optString(KEY_CITY)

    fun toJson(): JsonData {
        val jsonData = JsonData.newMap()
        jsonData.put(KEY_SEX, sex)
        jsonData.put(KEY_VERIFY, verify)
        jsonData.put(KEY_MIN_AGE, minAge)
        jsonData.put(KEY_MAX_AGE, maxAge)
        jsonData.put(KEY_PROVINCE, province)
        jsonData.put(KEY_CITY, city)
        return jsonData
    }

    fun update(key: String, value: String) {
        when (key) {
            KEY_SEX -> sex = value
            KEY_VERIFY -> verify = value
            KEY_MIN_AGE -> minAge = value
            KEY_MAX_AGE -> maxAge = value
            KEY_PROVINCE -> province = value
            KEY_CITY -> city = value
        }
    }
}